using System;
using System.Collections.Generic;
using System.Windows.Forms;
using KnowledgeNotebook.DAL;

namespace KnowledgeNotebook.Forms
{
    public partial class frmExportSetting : Form
    {
        private string _fieldValue;

        private List<string> _fields;
        private List<int> _chkindex;
        private static string _sql;
        private static int _count;

        public frmExportSetting()
        {
            InitializeComponent();
            _fieldValue = String.Empty;
            _fields=new List<string>();
            _chkindex=new List<int>();
            _sql = "select from " + frmMain.MainForm.TableName;
            _count = 0;
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void frmExportSetting_Load(object sender, EventArgs e)
        {
            DatabaseOption dbo=new DatabaseOption();
            List<string> fields = dbo.GetFields(frmMain.MainForm.TableName);


            if (fields!=null && fields.Count>0)
            {

                for (int i = 0; i < fields.Count; i++)
                {
                    dataGridFields.Rows.Add();
                    dataGridFields.Rows[i].Cells[1].Value = fields[i];
                }

            }
        }

        private void dataGridFields_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex==0)
            {
                
                //_sql = txtsql.Text;
                //string selectfield = String.Empty;
                DataGridViewCheckBoxCell chkCell = dataGridFields.Rows[e.RowIndex].Cells[0] as DataGridViewCheckBoxCell;
                if (chkCell != null)
                {
                    _fieldValue = dataGridFields.Rows[e.RowIndex].Cells[1].Value+", ";
                    if (chkCell.Value.ToString()=="True")
                    {
                        _fields.Add(_fieldValue);
                       // 
                        _count++;
                        if (_count == 1)
                        {
                            _sql = _sql.Insert(_sql.LastIndexOf("from "), _fieldValue);
                            _sql = _sql.Replace(_sql.Substring(_sql.LastIndexOf(","), 1), String.Empty);
                        }
                        else
                        {

                           // _sql = _sql.Insert(_sql.LastIndexOf("from "), ", ");
                            _sql = _sql.Insert(_sql.LastIndexOf("from "), _fieldValue);
                            _sql.Remove(_sql.LastIndexOf(","), 1);//_sql = _sql.Replace(_sql.Substring(_sql.LastIndexOf("from ") - 2, 1), " ");
                        }
                        txtsql.Text = _sql;//if (txtsql.Text.IndexOf("select ")==-1 )//{
                        //    txtsql.Text = txtsql.Text.Insert(0, "select ");

                        //}

                        // txtsql.Text = txtsql.Text + _fieldValue;

                        //                    txtsql.Text = txtsql.Text.Replace(
                        //txtsql.Text.Substring(txtsql.Text.LastIndexOf(","), 1), String.Empty);
                    }
                    else
                    {
                        int index = _fields.IndexOf(_fieldValue);
                        if (_fields!=null && index!=-1)
                        {string tmp = _fields[index];
                            txtsql.Text = txtsql.Text.Replace(tmp, String.Empty);
                            _count--;_sql = _sql.Replace(tmp, String.Empty);_fields.RemoveAt(index);
                        }
                        
                        _fieldValue = String.Empty;
                    }




                }

            }
        }

        private void btnTest_Click(object sender, EventArgs e)
        {

        }

        private void dataGridFields_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dataGridFields.IsCurrentCellDirty)
            {
                dataGridFields.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

        private void chkCustom_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCustom.Checked)
            {
                btnOK.Enabled = false;
                btnTest.Enabled = true;
                txtsql.Enabled = true;
                txtsql.Text = String.Empty;
            }
            else
            {
                btnOK.Enabled = true;
                btnTest.Enabled = false;
                txtsql.Enabled = false;
                txtsql.Text = String.Empty;
            }
        }
    }
}